package com.example.demo.rocketmq_client;

import com.alibaba.fastjson.JSON;
import com.example.demo.VO.User;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.Random;

public class Producer {
    public static void main(String[] args) throws Exception {

        //1.创建消息生产者producer，并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2.指定Nameserver地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //3.启动producer
        producer.start();

        //4.创建消息对象，指定主题Topic、Tag和消息体
        /**
         * 参数一：消息主题Topic
         * 参数二：消息Tag
         * 参数三：消息内容
         */
        String key = String.valueOf(new Random().nextInt(5));
        User user = new User();
        user.setId(key);
        user.setName("消息");
        Message msg = new Message("Topic1", "Tag1", JSON.toJSONString(user).getBytes());
        msg.setKeys(key);

        //指定延迟时间
        msg.setDelayTimeLevel(1);

        //5.发送消息
        SendResult result = producer.send(msg);
        //发送状态
        SendStatus status = result.getSendStatus();
        System.out.println("发送结果:" + JSON.toJSONString(status));

        MessageQueue m = new MessageQueue();
        m.getTopic();
        m.getBrokerName();
        m.getQueueId();


        //6.关闭生产者producer
        producer.shutdown();
    }

}
